layui.config({
    base: "static/layui/module/"
}).extend({
	opTable: 'opTable/opTable'
}).use(['form','layer','jquery','table','laydate','upload','element','opTable'],function(){
	var form = layui.form,
		layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        upload = layui.upload,
        laydate =layui.laydate,
        element = layui.element,
        opTable=layui.opTable;

	//加载页面日期控件
	laydate.render({
		elem: '#yearAndMonth'
		,format: 'yyyy-MM' //可任意组合
		,type: 'month'
		,ready: function (date) {
			$(".layui-laydate").on('click', 'ul li', function () {
				$(".layui-laydate").remove();
			});
		}
		,change: function (value) {
			$("#yearAndMonth").val(value);
		}
	});

	$("#search").click(function (){
		var yearAndMonth=$("#yearAndMonth").val();
		if(yearAndMonth == ''){
			top.layer.msg('请选择年度期间');
			return
		}
		loadTable();
	});

	function loadTable(){
		var arr = $("#yearAndMonth").val().split("-");
		var indx;
		$.ajax({
			url: "/manage/voucher/master_list",
			type: "post",
			data: {iyear: arr[0], iperiod: arr[1], inoid: $("#inoid").val()},
			async:true,
			beforeSend: function () {
				indx = top.layer.msg('数据查询中，请稍后！', {icon: 16, time: false, shade: 0.8});
			},
			success: function (data) {
				top.layer.close(indx);
				opTable.render({
					elem:'#table'
					,page: true //开启分页
					,limit: 20
					,limits: [20,30,40,50,60,70,80,90,100]
					,loading: true
					,cols: [[ //表头
						{type:'numbers', title:'序号', minWidth:60},
						{field: 'prepareddate', title: '制单日期', minWidth:110},
						{field: 'no', title: '凭证号', minWidth:80},
						{field: 'attachment', title: '附单据数', minWidth:90},
						{field: 'explanation', title: '摘要', minWidth:320},
						{field: 'md', title: '借方合计', minWidth:100},
						{field: 'mc', title: '贷方合计', minWidth:100},
						{field: 'cbill', title: '制单人', minWidth:90},
						{field: 'ccheck', title: '审核人', minWidth:90},
						{field: 'dauditdate', title: '审核日期', minWidth:110},
						{field: 'cbook', title: '记账人', minWidth:90},
						{field: 'iyear', title: '年度', minWidth:80},
						{field: 'iperiod', title: '会计期间', minWidth:90}
					]]
					,text: {
						none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
					}
					,data: data.data.data
					,toolbar:'#tableToolbar'
					,defaultToolbar: ['filter']
					,openType: 1
					,openTable: function (itemData) {
						return {
							elem: '#child_1_' + itemData.LAY_INDEX
							, id: 'child_1_' + itemData.LAY_INDEX
							, url: '/manage/voucher/detail_list'
							, where: {iyear: itemData.iyear, iperiod: itemData.iperiod, inoid: itemData.no}
							, method:'post'
							, openVisible: false
							, cols: [[
								{field: 'iyear', title: '年度', minWidth:80},
								{field: 'iperiod', title: '会计期间', minWidth:90},
								{field: 'no',title:'凭证号',minWidth:90},
								{field: 'detailindex',title:'行号',minWidth:80},
								{field: 'accsubj',title:'会计科目',minWidth:100},
								{field: 'debitamount',title:'借方金额',minWidth:100},
								{field: 'creditamount',title:'贷方金额',minWidth:100},
								{field: 'explanation',title:'摘要',minWidth:200},
								{field: 'deptcode',title:'辅助核算-部门',minWidth:150},
								{field: 'deptname',title:'辅助核算-部门名称',minWidth:150},
								{field: 'custcode',title:'辅助核算-客户',minWidth:150},
								{field: 'custname',title:'辅助核算-客户名称',minWidth:150},
								{field: 'psncode',title:'辅助核算-人员',minWidth:150},
								{field: 'psnname',title:'辅助核算-人员名称',minWidth:150},
								{field: 'jobcode',title:'辅助核算-项目',minWidth:150},
								{field: 'jobname',title:'辅助核算-项目名称',minWidth:150},
								{field: 'cashflow',title:'现金流量项目',minWidth:130},
								{field: 'money',title:'现金流量原币金额',minWidth:150},
							]]
						}
					}
				});
				if(data.status){
					$("#upload_msg").css("color","green");
				}else{
					$("#upload_msg").css("color","red");
				}
				$("#upload_msg").html(data.msg);
			}
			,error(){
				top.layer.msg('系统异常');
				top.layer.close(indx);
			}
		})
	}

	table.on('toolbar(table)', function(obj) {
		var tableCheckData = table.checkStatus(obj.config.id).data;
		switch (obj.event) {
			case 'upload':
				var yearAndMonth=$("#yearAndMonth").val();
				if(yearAndMonth == ''){
					top.layer.msg('请选择年度期间');
					return
				}
				top.layer.confirm('请再次确认是否上传'+yearAndMonth+'月份的凭证数据?', {icon: 3, title:'提示'}, function(index){
					var arr = yearAndMonth.split("-");
					var indx;
					$.ajax({
						url: "/manage/voucher/upload",
						type: "post",
						data: {iyear: arr[0], iperiod: arr[1]},
						async: true,
						beforeSend: function () {
							indx = top.layer.msg('数据上传中，时间可能有点长，请耐心等待！', {icon: 16, time: false, shade: 0.8});
						},
						success: function (d) {
							top.layer.close(indx);
							if(d.code == '200'){
								top.layer.msg("上传成功",{icon:1});
								layer.close(index);
							}else{
								top.layer.msg("上传失败，"+d.msg,{icon:2});
							}
							loadTable();
						}
					})
				})
				break;
		}
	})
})
